package com.kimstore.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name = "item")
public class Item {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String serial;
    
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "product_id")
    private Product product;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "purchase_detail_id", nullable = true)
    private PurchaseDetail purchaseDetail;
    
    @Column(nullable = true)
    private String remarks;
    
    @Embedded
    @ElementCollection
    @LazyCollection(LazyCollectionOption.FALSE)
    @CollectionTable(name = "item_status")
    @OrderBy("date asc")
    private List<ItemStatus> history;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "repair_detail_id", nullable = true)
    private RepairDetail repairDetail = null;
    
    
    @Transient
    private Date inDate;
    
    @Transient
    private Date outDate;
    
    @Column(name="latest_status")
    private String latestStatus;
    
    public Item() {
    }

    public Item(Product product, String serial) {
        this.product = product;
        this.serial = serial;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public List<ItemStatus> getHistory() {
        return history;
    }

    public void setHistory(List<ItemStatus> history) {
        this.history = history;
    }

    public void addHistory(ItemStatus itemStatus) {
        if (history == null) {
            history = new ArrayList<ItemStatus>();
        }

        history.add(itemStatus);
    }

    public PurchaseDetail getPurchaseDetail() {
        return purchaseDetail;
    }

    public void setPurchaseDetail(PurchaseDetail purchaseDetail) {
        this.purchaseDetail = purchaseDetail;
    }

    public RepairDetail getRepairDetail() {
        return repairDetail;
    }

    public void setRepairDetail(RepairDetail repairDetail) {
        this.repairDetail = repairDetail;
    }

    public Date getInDate() {
        return inDate;
    }

    public void setInDate(Date inDate) {
        this.inDate = inDate;
    }

    public Date getOutDate() {
        return outDate;
    }

    public void setOutDate(Date outDate) {
        this.outDate = outDate;
    }

    public String getLatestStatus() {
        return latestStatus;
    }

    public void setLatestStatus(String latestStatus) {
        this.latestStatus = latestStatus;
    }
}
